// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// k [author] rhyme_lph
// e [email]  rhymelph@gmail.com
// k [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/entities/sub_billing_item_entity.dart';
import 'package:finance_app/src/provider/setting/subscription/sub_billing_provider.dart';
import 'package:finance_app/src/provider/setting/subscription_provider.dart';
import 'package:finance_app/src/utils/string_extension.dart';
import 'package:finance_app/src/widgets/list_item/sub_billing_header_item.dart';
import 'package:finance_app/src/widgets/list_item/sub_company_item.dart';
import 'package:flutter/material.dart';

class SubBillingView extends StatefulWidget {
  @override
  _SubBillingViewState createState() => _SubBillingViewState();
}

class _SubBillingViewState extends State<SubBillingView>
    with
        StatefulPresenter<SubBillingView, SubscriptionProvider,
            SubBillingProvider> {
  @override
  Widget buildWidget(BuildContext context) => ListView(
        padding:
            EdgeInsets.only(left: getDp(25), right: getDp(28), top: getDp(20)),
        children: [
          AppSpaces.h15,
          listBuilder<int>(
            selector: (_, __) => __.upcomingBills.length,
            builder: (BuildContext context, int value, Widget child) =>
                value > 0
                    ? Column(
                        children: [
                          SubBillingHeaderItem(
                            title: S.current.bill_under_this_account,
                          ),
                          AppSpaces.h15,
                          listBuilder<int>(
                            selector: (_, __) => __.upcomingBills.length,
                            builder: (BuildContext context, int value,
                                    Widget child) =>
                                Column(
                              children: List.generate(value, _buildCompanies),
                            ),
                          ),
                        ],
                      )
                    : Container(),
          ),
        ],
      );

  Widget _buildCompanies(int index) {
    return listBuilder<SubBillingItemEntity>(
      selector: (_, __) => __.upcomingBills[index],
      builder:
          (BuildContext context, SubBillingItemEntity value, Widget child) =>
              Padding(
        padding: EdgeInsets.only(bottom: getDp(15)),
        child: SubCompanyItem(
            companyName: value.companyName,
            planName: value.originalPrice == 0
                ? S.current.free_trial
                : value.planName.toCapitalized(),
            isExpired: value.originalPrice == 0
                ? null
                : DateTime.parse(value.currentDate)
                    .isAfter(DateTime.parse(value.endAt)),
            onTap: () => provider.onSelectCompany(value)),
      ),
    );
  }
}
